package com.wink_172.library.fragment

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.wink_172.library.R
import com.wink_172.library.model.Constants
import com.wink_172.library.model.json.CustomEventBus
import com.wink_172.library.view.CustomProgressDialog
import com.wink_172.library.view.ProgressLoading
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author: fcg
 * @date: 2020-11-03
 * Description:
 */
open class BaseFragment : Fragment(), View.OnClickListener {
    protected var progressDialog: CustomProgressDialog? = null
    open  var handler: MyHandler? = null

    inner class MyHandler : Handler() {
        override fun handleMessage(msg: Message) {
            handlerCallBack(msg)
        }
    }


    /**
     * 执行顺序start
     * */
    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        handler = MyHandler()
        EventBus.getDefault().register(this)
        return super.onCreateView(inflater, container, savedInstanceState)


    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessages(messageBus: CustomEventBus) {

        when(messageBus.type){

        }
    }

    override fun onClick(p0: View) {

    }

    override fun onStart() {
        super.onStart()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onDetach() {
        super.onDetach()
    }
    /**
     * 执行顺序end
     * */
    open fun handlerCallBack(msg: Message) {
        when (msg.what) {
            Constants.EVENT_01 -> {
            }
        }
    }


    open fun showProgressDialog(vararg args: Any?) {
        if (progressDialog == null) {
            progressDialog = CustomProgressDialog(activity!!, R.style.CustomProgressDialog)

            progressDialog!!.setContentView(R.layout.progress_loading)
            progressDialog!!.show()

            val progress_loading=progressDialog!!.findViewById<View>(R.id.progress_loading) as ProgressLoading
            progress_loading.tv_message.visibility=View.GONE

            var touch = false

            if (args != null) {
                when( args.size){
                    1->{
                        touch = args[0] as Boolean

                    }
                    2->{
                        touch = args[0] as Boolean
                        val str_text = args[1] as String

                        progress_loading.tv_message.setText(str_text)
                        progress_loading.tv_message.visibility=View.VISIBLE
                    }
                    3->{
                        touch = args[0] as Boolean
                        val str_text = args[1] as String
                        val show_bg = args[2] as Boolean

                        progress_loading.tv_message.setText(str_text)
                        progress_loading.tv_message.visibility=View.VISIBLE

                        if(show_bg){
                            progress_loading.background=activity!!.resources.getDrawable(R.drawable.toaststyle)
                        }
                    }
                }


            }
            progressDialog!!.setCancelable(touch)
            progressDialog!!.setCanceledOnTouchOutside(touch)
            progressDialog!!.window!!.attributes.gravity = Gravity.CENTER

        } else {
            progressDialog!!.show()
        }
    }

    open  fun hideProgressDialog() {
        if (progressDialog != null&&activity!=null&&activity!!.isFinishing==false) {
            progressDialog!!.dismiss()
            progressDialog = null
        }
    }

    /**
     * 请不要再外部调用MyHandler().sendMessage
     * 它是每调用一次就生成一个新的handler   不是同一个handler 根本removeMessages不了
     * 请使用getHandle()方法
     * */
    fun getHandle(): MyHandler {
        return handler!!;
    }


    companion object {
        private const val TAG = "BaseFragment"
    }
}